unit goalkeeperUnit;

interface

uses common, ballUnit, PlayerUnit;

type
  GoalKeeper = class
    function move_before(timeLeft, goalKeeperPosition, playerDirection : integer) : integer;
    function move_after(timeLeft, goalKeeperPosition, hitDirection, ballY : integer) : integer;
  end;

var
  y0, y1:integer;


implementation

{ GoalKeeper }

function GoalKeeper.move_after(timeLeft, goalKeeperPosition, hitDirection,
  ballY: integer): integer;
var vy, vx:integer;
    first:boolean;
begin
  if first then
  begin
    y0:=y1;
    y1:=ballY;
    first:=false;
  end;
  vy:=y0-y1;
  if vy = 0 then
    vy:=DISTANCE;
  move_after:=HANDS;
  if (hitDirection > goalKeeperPosition) then
    move_after := STEP_RIGHT
  else if (hitDirection < goalKeeperPosition) then
    move_after := STEP_LEFT;
  if (abs(hitDirection - goalKeeperPosition)<=HANDS_WIDTH) then
    move_after:=HANDS;
  if((abs(hitDirection - goalKeeperPosition)/JUMP_LENGTH) <= (2*(DISTANCE-ballY)/vy + 1/2)) and ((abs(hitDirection - goalKeeperPosition)/JUMP_LENGTH) >= 2*(((DISTANCE-ballY)/vy) - 1/2)) and (hitDirection - goalKeeperPosition>0) then
    move_after:=JUMP_RIGHT;
  if((abs(hitDirection - goalKeeperPosition)/JUMP_LENGTH) <= (2*(DISTANCE-ballY)/vy + 1/2)) and ((abs(hitDirection - goalKeeperPosition)/JUMP_LENGTH) >= 2*(((DISTANCE-ballY)/vy) - 1/2)) and (hitDirection - goalKeeperPosition<0) then
    move_after:=JUMP_LEFT;

  if (abs(DISTANCE - ballY) <= abs(vy)) then
  begin
    y0:=0;
    y1:=0;
  end;
end;

function GoalKeeper.move_before(timeLeft, goalKeeperPosition,
  playerDirection: integer): integer;
begin
  if (playerDirection> goalkeeperPosition) then
    move_before:=STEP_RIGHT
  else
    move_before:=STEP_LEFT;
end;

end.
